﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using WordPress.Net;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            sourceLink.Text = flickrBrowser.Url.ToString();

            MatchCollection data = Regex.Matches(flickrBrowser.DocumentText, "class=\"photoImgDiv\">\n<img src=\"([^\"]+)", RegexOptions.IgnoreCase);
            foreach (Match treffer in data) pictureLinkFlickr.Text = treffer.Groups[1].Value.ToString();
        }

        private void buttonPictureUpload_Click(object sender, EventArgs e)
        {
            imageshackBrowser.Document.GetElementById("toggle1").InvokeMember("click");
            imageshackBrowser.Document.GetElementById("transurl").InnerText = pictureLinkFlickr.Text;
            imageshackBrowser.Document.GetElementById("upload-options-resizing").InvokeMember("click");
            imageshackBrowser.Document.GetElementById("optsize").SetAttribute("value", "150x150");
            imageshackBrowser.Document.GetElementById("rembar").SetAttribute("value", "1");
            imageshackBrowser.Document.GetElementById("upform").InvokeMember("submit");
        }

        private void getPictureLinkButton_Click(object sender, EventArgs e)
        {
            string sourceCode = imageshackBrowser.DocumentText;

            MatchCollection data = Regex.Matches(sourceCode, "<label><a href=\"([^\"]+)", RegexOptions.IgnoreCase);
            pictureLinkImageshack.Text = data[1].Groups[1].Value.ToString();
        }

        private void editPostButton_Click(object sender, EventArgs e)
        {
            WP wp = new WP();
            Post editPost = wp.GetRecentPosts(1)[0];

            string pictureCode;
            pictureCode = "<?php if(stristr($_SERVER['HTTP_USER_AGENT'],'google')==false) echo('[caption id=\"\" align=\"alignright\" width=\"150\" caption=\" \"]<img title=\"" + editPost.title + "\" src=\"" + pictureLinkImageshack.Text + "\" alt=\"\" width=\"150\" height=\"100\" /><small><a href=\"" + sourceLink.Text + "\">via Flickr</a></small>[/caption]'); ?>";

            editPost.description = pictureCode + editPost.description;

            wp.EditPost(editPost, true);
        }
    }
}
